package com.team2.cyber_eye;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.team2.cyber_eye.ActivityHelpers.Util;
import com.team2.shared.SharedConstants;

public class SearchByText extends CyberActivity  {
	
	//public List<String> actual_list;
	public String selectedCategory = "";
	ImageView image;
	EditText productName;
	public String fromIQE = "";
	public String filePath = "";
	public Bitmap bmp = null;
	public String iqeOrBarcode = "";
	private String fromHistory = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.activity_search_by_text, "PRODUCT SEARCH");

		Bundle extras = this.getIntent().getExtras();
		image = (ImageView)this.findViewById(R.id.product_image);
		productName = (EditText)this.findViewById(R.id.editText1);
		
		if (extras != null)
		{		
			fromIQE = extras.getString("FromIQE");
			String text = extras.getString("text");
			fromHistory = extras.getString("fromHistory");
			
			if(fromIQE.equals("yes")){
				
				iqeOrBarcode = (String)extras.get("iqeOrBarcode");
				
				if(iqeOrBarcode.equals("iqe")){
					Uri uri = this.getIntent().getData();
					
					 InputStream is;
			            
			            try {
							is = this.getContentResolver().openInputStream(uri);
							
							bmp = Util.decodeSampledBitmapFromResourceMemOpt(is, 100, 100);
							
						} catch (FileNotFoundException e) {
							
							Util.DisplayIssue(getApplicationContext(), "Error compressing image");
						}
					
					filePath = Util.FilePath(uri);
					
					
					if (bmp != null)
					{						
						Bitmap rotatedBitmap = Util.RotateBitmap(bmp, filePath);
						
						if(rotatedBitmap != null && fromHistory == null){
							image.setImageBitmap(rotatedBitmap);
						}else{
							image.setImageBitmap(bmp);
						}
						
					}
				} else if(iqeOrBarcode.equals("barcode")){
					bmp = (Bitmap)extras.get("photo");
					
					Bitmap rotatedBitmap = Util.RotateBitmap(bmp, filePath);
					
					if(rotatedBitmap != null && fromHistory == null){
						image.setImageBitmap(rotatedBitmap);
					}else{
						image.setImageBitmap(bmp);
					}
					
				}
							
				if (text != null)
				{		
					productName.setText(text);
				}
			}
		}
		
		Button searchbtn = (Button) findViewById(R.id.button1);
		
		searchbtn.setOnClickListener(new View.OnClickListener() { 
			@Override public void onClick(View v) 
				{ 
					SearchClick(); 
				} 
			});
		
		Spinner spinnerCategory = (Spinner) findViewById(R.id.categorySpinner);
		ArrayAdapter<CharSequence> adapterCategory = ArrayAdapter.createFromResource(this, R.array.category_array, android.R.layout.simple_spinner_item);
		adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerCategory.setAdapter(adapterCategory);
		
		spinnerCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int pos,
					long id) {
			
				if(!parent.getItemAtPosition(pos).equals("Select")){
					selectedCategory = parent.getItemAtPosition(pos).toString();
					
					Context context = getApplicationContext();
					CharSequence text = selectedCategory;
					int duration = Toast.LENGTH_SHORT;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				}else{
					selectedCategory = parent.getItemAtPosition(pos).toString();
				}
			
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub				
			}

		});
				
		this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		
		CreateAdvertisement();
		
	}
	
	private void SearchClick()
	{
		String SearchText = productName.getText().toString();
		SearchText = SearchText.replace('\n', '+').replace('\t', '+').replace('\r', '+').replace(' ', '+');
		
		if(Util.CheckInternet(this)){
			if(SearchText.length() == 0)
				Util.DisplayMessage(this, "A product name is required");
			else if(selectedCategory.equals("Select"))
				Util.DisplayMessage(this, "Select a category");
			else
			{
				//fixme actualy use category
				Intent intent = new Intent();
				intent.setClass(SearchByText.this, Processing.class);
				intent.putExtra("text", SearchText);
				intent.putExtra("filePath", filePath);
				intent.putExtra("category", selectedCategory);
				intent.putExtra("fromIQE", fromIQE);
				
				if(iqeOrBarcode == null || iqeOrBarcode.equals("")){
					iqeOrBarcode = "none";
				}
				intent.putExtra("iqeOrBarcode", iqeOrBarcode);
				startActivity(intent);
			}
		}			
	}
}
